#!/usr/bin/python2
# Julien Michel june 2007
#  usage is getAverageProfile.py file1.dat file2.dat file3.dat
# where fileX can be an electron density profile, or a lateral pressure profile.
# can probably be used for other outputs like electron pressure profile etc...

import os,sys

averages = []

resized = False
for file in sys.argv[1:]:
    stream = open(file,'r')
    buffer = stream.readlines()
    stream.close()

    if not resized:
        #check = buffer[0].split()
        check = len(buffer)
        for x in range(0,check):
            averages.append([])
        resized = True
    #print averages
    for y in range(0,len(buffer)):
        elems = buffer[y].split()
        nprops = len(elems)
        for x in range(0,len(elems)):
            #print elems[x]
            averages[y].append(float(elems[x]))

    #print file
    #print len(averages[0])
    #print len(averages)
    #for y in range(0,len(averages[0])):
    #    for x in range(0,len(averages)):
    #        #print x,y
    #        print averages[x][y]
    #    #for val in av:
    #    #    print val
    #    #sys.exit(-1)
# Now collapse
for line in averages:
    avgs = []
    for x in range(0,nprops):
        prop = 0
        y = x
        count = 0
        while (y < len(line)):
            prop += line[y]
            count +=1
            #print y,prop
            y += nprops
        prop /= count
        #print prop
        avgs.append(prop)
    #print line
    str = " "
    for val in avgs:
        str += "%8.5f " % val
    print str
    #sys.exit(-1)
